﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Cheetah_Fast_Banking_App
{
    public partial class FormOpenAccount : Form
    {
        #region Constructor
        public FormOpenAccount()
        {
            InitializeComponent();
        }
        #endregion // Constructor

        #region Form Events
        private void buttonCancel_Click(object sender, EventArgs e)
        {
            // Close the form without making changes.
            this.Close();
        }

        private void buttonOpenAccount_Click(object sender, EventArgs e)
        {
            // Attempt to open account.
            string accountName = textBoxAccountName.Text;
            string password = textBoxPassword.Text;
            string confirmedPassword = textBoxConfirmPassword.Text;

            string error = DataSourceSingleton.GetInstance().OpenAccount(accountName, password, confirmedPassword);
            if (string.IsNullOrEmpty(error))
            {
                // No error was encountered--close the form.
                MessageBox.Show("Account was successfully created.", "Account Created",
                    MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.Close();
            }
            else
            {
                // Display error message and keep the form open.
                MessageBox.Show(error, "Error Opening Account", MessageBoxButtons.OK, MessageBoxIcon.Error);
                textBoxPassword.Text = "";
                textBoxConfirmPassword.Text = "";
                textBoxAccountName.Focus();
            }
        }
        #endregion // Form Events
    }
}
